var SpriteManager = require("SpriteManager");
var EffectManager = require("EffectManager");
var ChapterConfig = require("ChapterConfig");
var ChangeManager = require("ChangeManager");
var MapManager = require("MapManager");
var EventManager = require("EventManager");
var GameManager = require("GameManager");
var RewardManager = require("RewardManager");

cc.Class({
    extends: cc.Component,

    properties: {
    },
	
	moveAll(){
		
		this.effectMast.active=true;
		
		this.messageDisplay.node.stopAllActions();
		
		this.messageDisplay.node.setScale(1);
		
		this.messageDisplay.node.setPosition(0,-700);
		
		this.messageDisplay.string = '没有可移动块！'
		
		this.messageDisplay.node.runAction(cc.moveBy(3,0,700));
		
		this.node.runAction(cc.sequence(cc.delayTime(3.5), this.toScoreCallback));
		
	},
	
	gameOver(){
		
		if(this.playerType!=3){
		
			if(this.lastStep<=0){
				return true;
			}else{
				return false;
			}
		}else{
			
			if(this.lastStep<=0||this.complete+this.completeAdd>=this.levelTarget){
				return true;
			}else{
				return false;
			}
		}
	},
	
	initBaseGame(type){
		
		this.initBaseParam(type);
		
		this.initManagers();
		
		this.initGameParam();
		
		this.initGameArr();
		
		this.initMapParam();
		
		this.initGameController();
		
		this.showUi();
		
		this.initCallBackFunc();
	},
	
	//回调函数
	initCallBackFunc(){
		this.exChangeCallback = cc.callFunc(function () {
				this.changeManager.change(this);
		}, this);
		
		this.exChangeBackCallBack = cc.callFunc(function () {
				this.goon();
		}, this);
		
		this.toScoreCallback = cc.callFunc(function () {
				this.toScoreUI();
		}, this);  
	},
	
	//游戏操作
	initGameController(){
		this.eventManager.addTouchStart(this);
		
		this.eventManager.addTouchMove(this);
	},
	
	//基本参数
	initBaseParam(type){
		this.playerType = type;//游戏模式  1--积分  2--竞速  3--关卡
		
		this.level=(window.level)?window.level:0;//游戏关卡
		
		if(type==1){
			this.scoreUiName="score";
		}else if(type==2){
			this.scoreUiName="scoreByTime";
		}else if(type==3){
			this.scoreUiName="scoreByChapter";
		}
	},
	
	//游戏容器参数
	initGameArr(){
		this.mapPointFrame = this.spriteManager.getMapPointFrame(this);
		
		this.signFrame = this.spriteManager.getSignFrame(this);
		
		this.maskFrame = this.spriteManager.getMaskFrame(this);
		
		this.blockPointFrameArr = this.spriteManager.getBlockPointFrameArr(this);
		
		this.mapPointArr = [];
		
		this.mapPointForDisappearArr=[];
		
		this.mapPointNoMap = {};
		
		this.mapPointForEffectArr=[];
		
		this.boomArr=[];
		
		this.effectArr=[];
		
		this.blockNodeForRemove=[];
		
		this.selectedMask = null;
		this.selectedBlock=null;
		this.selectedBlock2=null;
		
		
		this.iceFrame = this.spriteManager.getIceFrame(this);
		this.hummerFrame = this.spriteManager.getHummerFrame(this);
		this.magicFrame = this.spriteManager.getMagicFrame(this);
	},
	
	//游戏流程参数
	initGameParam(){
		this.happyTime = 0;//0--游戏中  1--游戏目标完成消除特殊  2--步数奖励
		
		this.lastScore = (window.lastScore)?window.lastScore:0;
		
		this.score = (window.score)?window.score:0;
		
		this.scoreadd = 0;
		
		this.targetComplete = 0;
		
		this.testCreateNewNmb = 0;
		
		this.levelConfig = this.chapterConfig.levelConfig[this.level];
		
		this.levelTarget = this.levelConfig.target;
		
		this.lastStep = this.levelConfig.step;
		
		//#############竞速模式
		if(this.playerType==2){
			
			this.lastScore = 0;
		
			this.score = 0;
		
			this.levelTarget = cc.sys.localStorage.getItem("hightestScore")?cc.sys.localStorage.getItem("hightestScore"):0;//历史最高分
		
			this.lastStep = 3000;
		
			this.usedStep = 0;
		}
		
		//##############关卡模式
		if(this.playerType==3){
			
			this.levelConfig = this.chapterConfig.chapterConfig[this.level];
		
			this.empty = this.levelConfig.empty;
		
			this.ice = this.levelConfig.ice;
		
			this.targetType1 = this.levelConfig.targetType1;
		
			this.targetType2 = this.levelConfig.targetType2;
		
			this.complete = 0
		
			this.completeAdd = 0;
			
			this.lastStep = this.levelConfig.step;
			
			this.levelTarget = this.levelConfig.target;
		}
		
	},
	
	//地图尺寸参数
	initMapParam(){
		this.shapeNmb=this.levelConfig.shape;
		this.mapType=this.levelConfig.maptype;
		
		this.width = 375;
		this.height = 667;
		this.widthNmb = this.levelConfig.width;
		this.heightNmb = this.levelConfig.height;
		this.mapScale=0.6;
		this.blockScale=0.55;
		this.w = 54;
		this.w2 = 24;
		this.h = 48;
		
		
		//#################竞速模式
		if(this.playerType==2){
			this.shapeNmb=5;
			this.mapType=1;
			this.maxItemNmb=3;
		
			this.widthNmb = 4;
			this.heightNmb = 14;
			this.mapScale=0.7;
			this.blockScale=0.6;
			this.w = 63;
			this.w2 = 28;
			this.h = 56;
			
		}
		
		//################关卡模式
		if(this.playerType==3){
			
			this.shapeNmb=5;

		}
		
		
		this.tgnmb = this.h/2/((this.w-this.w2)/2+this.w2);
		
		this.startx = (this.width-this.widthNmb*this.w-(this.widthNmb-1)*this.w2)/2+this.w/2;
		this.starty = this.height-(this.height-this.heightNmb/2*this.h)/2-this.h/2;
		
		this.mapManager.initMap(this);
	},
	
	//游戏管理对象
	initManagers(){
		this.chapterConfig = new ChapterConfig();
		
		this.spriteManager = new SpriteManager();
		
		this.effectManager = new EffectManager();
		
		this.changeManager = new ChangeManager();
		
		this.mapManager = new MapManager();
		
		this.eventManager = new EventManager();
		
		this.gameManager = new GameManager(this);
		
		this.rewardManager = new RewardManager(this);
	},
	
	showUi(){//游戏界面开场变亮特效
		
		if(this.playerType!=2)
			this.effectMast.active=false;
		
		this.goonDisplay.node.active=false;
		this.backDisplay.node.active=false;
	
		this.canvas.opacity =0;
		this.canvas.runAction(cc.fadeIn(1));
	},
	
	createNode(x,y,mapframe,scale){//新建节点
		return this.spriteManager.createNode(x,y,mapframe,scale,this);
	},
	
	disappear(){
		this.gameManager.disappear();
	},
	
	clearMapForDisapper(){
		this.gameManager.clearMapForDisapper();
	},
	
	pushMapPointForDisappear(block){
		return this.gameManager.pushMapPointForDisappear(block);
	},

	showMenu(){//展示游戏界面
		this.timeCost=false;
		this.effectMast.active=true;
		this.goonDisplay.node.active=true;
		this.backDisplay.node.active=true;
	},
	
	hideMenu(){//关闭游戏界面
		this.timeCost=true;
		this.effectMast.active=false;
		this.goonDisplay.node.active=false;
		this.backDisplay.node.active=false;
	},
	
	getScore(){
		return this.score;
	},
	
	getLevel(){
		return this.level/1;
	},
	
	getTarget(){
		return this.levelTarget;
	},
	
	getStep(){
		return this.lastStep;
	},
	
	getComplete(){
		return this.complete;
	},
	
	//文字提示
	showMessage(message){
		
		this.messageDisplay.node.stopAllActions();
		
		this.messageDisplay.node.setScale(1);
		
		this.messageDisplay.node.setPosition(-400,0);
		
		this.messageDisplay.string = message;
		
		this.messageDisplay.node.runAction(cc.sequence(cc.moveBy(0.2,400,0),cc.delayTime(1),cc.moveBy(0.2,400,0)));
		
	},
	
	//跳转结算页面
	toScoreUI(){
		if(this.playerType!=3){
			window.score = this.score;
			window.target =this.levelTarget;
			window.level = this.level;
			window.lastScore = this.lastScore;
		}else{
			window.score = this.score;
			//window.target =this.levelTarget;
			window.level = this.level;
			//window.lastScore = this.lastScore;
			window.passLevel = (this.complete>=this.levelTarget)
		}
		cc.director.loadScene(this.scoreUiName);
	}
});
